$(document).ready(function() {

    edit_order();
    delete_product();
    $("#reset").click(function() {
        $("#order_id").val(0);
        $('#order_name').val('');
        $('#order_des').val('').blur();
    });
    var act_submit = false;
    $("#submit").click(function() {
        if (act_submit === false) {
            act_submit = true;
            if ($('#order_name').val() !== "") {
                $.post("ajax/addOrder.php", {
                    order_id: $("#order_id").val(),
                    order_name: $('#order_name').val(),
                    order_des: $('#order_des').val()

                }, function(res) {
                    if (res.type == 0) {
                        $("#tbody_order_list").append(res.html);
                        alert(res.msg);
                        location.reload();
                    } else {
                        alert(res.msg);
                    }
                    act_submit = false;
                }, "json");
            } else {
                alert("Vui lòng điền tên order!");
                act_submit = false;
            }
        } else {
            return false;
        }
    });
    var killScroll = false;
    $(window).scroll(function() {
        if ($('#loadmore_order').val() == 1) {
            if ($(window).scrollTop() > ($(document).height() - $(window).height() - 100)) {
                $("#loading").show();
                if (killScroll === false) {
                    killScroll = true;
                    $.post("ajax/loadMoreOrder.php", {
                        page: $('#page_order').val()

                    }, function(res) {
                        $("#loading").hide();
                        $("#tbody_order_list").append(res.html);
                        $('#page_order').val(res.page);
                        $('#loadmore_order').val(res.load_more);
                        edit_order();
                        delete_product();
                        killScroll = false;

                    }, "json");
                } else {
                    return false;
                }
            }
        }
    });
});
function delete_product() {
    var act_del = false;
    $(".btn_delete_order").unbind('click');
    $(".btn_delete_order").click(function() {
        var btn_del = $(this);
        if (confirm("Bạn có chắc muốn xóa đợt hàng này?")) {
            if (act_del === false) {
                act_del = true;
                $.post("ajax/deleteOrder.php", {
                    order_id: $(this).attr('rel')
                }, function(res) {
                    act_del = false;
                    alert(res.msg);
                    if (res.type == 0) {
                        btn_del.parent().parent().remove();
                    }

                }, "json");


            } else {
                return false;
            }
        } else {
            return false;
        }
    });
}
function edit_order() {
    var act_edit = false;
    $(".btn_edit_order").unbind('click');
    $(".btn_edit_order").click(function() {
        if (act_edit === false) {
            act_edit = true;
            $.post("ajax/getOrderDetailById.php", {
                order_id: $(this).attr('rel')
            }, function(res) {

                if (res.type == 0) {
                    $("#order_name").val(res.zen_order_name);
                    $("#order_des").val(res.zen_order_description).blur();

                    $("#order_id").val(res.order_id);
                } else {
                    alert(res.msg);
                }
                act_edit = false;
            }, "json");


        } else {
            return false;
        }

    });
}


 